/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 *
 * @author computer
 */
public class edit extends HttpServlet {

    /**
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */

    String usrName, pwd, pwdHash, DBhash;
    int msgID, parentID, threadID, id, isAdmin = 0;
    Integer UID = 0;
    String subj, body;

    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException, SQLException, Exception {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        HttpSession session = request.getSession(true);
        try {
            Connect.init();
            Connection conn = Connect.getConnection();

            id = Integer.parseInt(request.getParameter("id"));

            out.println("<html>");
            out.println("<head>");
            out.println("<title>Threadr</title>");
            out.println("</head>");
            out.println("<body>");
            out.println("<table>");
            out.println("<tr>");
            if (session.getAttribute("uID") == null) {
                out.println("    <td bgcolor=\"#ebebeb\"> <!-- sidebar for not logged in-->");
                out.println("         <h2>Login</h2>");
                out.println("         <form id=\"login\" method=\"post\" action=\"Login\"><p>");
                out.println("              <label>Username<br />");
                out.println("	        <input type=\"text\" name=\"name\" id=\"name\" />");
                out.println("              </label><br />");
                out.println("              <label>Password<br />");
                out.println("	        <input type=\"password\" name=\"pwd\" id=\"pwd\" />");
                out.println("              </label><br />");
                out.println("              <input type=\"submit\" name=\"Login\" id=\"submit\" value=\"Login\" />");
                out.println("         </p></form>");
                out.println("         <p><a href=\"newacct.jsp\">Create Account</a></p>");
                out.println("     </td>");
            } else {
                UID = (Integer)session.getAttribute("uID");
                Statement stat3 = conn.createStatement();
                ResultSet user = stat3.executeQuery("SELECT * FROM t_Users WHERE UID = " + UID);
                while(user.next()) {
                    usrName = user.getString("username");
                    isAdmin = user.getInt("isAdmin");
                }
                out.println("<td bgcolor=\"#ebebeb\"> <!-- sidebar for logged in-->");
                out.println("     <h2>"+usrName+"</h2>");
                out.println("     <a href=\"newthread.jsp\">New Thread</a><br />");
                out.println("     <a href=\"logout\">Logout</a><br />");
                out.println("     <a href=\"delacct.jsp\">Delete Account</a>");
                if (isAdmin == 1)
                    out.println("<br/><a href=\"adminpanel\">Admin Panel</a>");
                out.println("</td>");
            }
            out.println("     <td>");
            out.println("         <h1><a href=\"index\">Threadr</a></h1>");
            out.println("         <br />");

            try
            {
               Statement stat = conn.createStatement();
               ResultSet messages = stat.executeQuery("SELECT * FROM t_Messages WHERE messageID = "+id);
               while(messages.next())
                   out.println("Editing: <br/><a href=\"thread?id=" +
                           id + "\">" + messages.getString("subj") + "</a><br/>");
               out.println("<form id=\"editAction\" method=\"post\" action=\"editAction\"><p>");
               out.println("          <label>Body (140 char limit)<br/>");
               out.println("              <input type=\"textarea\" name=\"body\" id=\"body\"/>");
               out.println("          </label><br/>");
               out.println("          <input type=\"hidden\" name=\"id\" id=\"id\" value=\"" + id +"\" />");
               out.println("          <input type=\"submit\" name=\"Submit\" id=\"submit\" value=\"Reply!\" />");
               out.println("</p></form>");
            }
            finally
            {
             conn.close();
            }

        } finally {
            out.println("</td>");
            out.println("</tr>");
            out.println("</table>");
            out.println("</body>");
            out.println("</html>");
            out.close();
        }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    //@Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        try {
            processRequest(request, response);
        } catch (SQLException ex) {
            Logger.getLogger(Login.class.getName()).log(Level.SEVERE, null, ex);
        } catch (Exception ex) {
            Logger.getLogger(Login.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    /**
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    //@Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        try {
            processRequest(request, response);
        } catch (SQLException ex) {
            Logger.getLogger(Login.class.getName()).log(Level.SEVERE, null, ex);
        } catch (Exception ex) {
            Logger.getLogger(Login.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    /**
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Index Servlet";
    }// </editor-fold>
}